﻿using Avalonia;
using Avalonia.Animation.Easings;
using Avalonia.Controls;
using Avalonia.Metadata;
using AvaloniaApplication.Core;
using ReactiveUI;
using System;
using System.ComponentModel;
using System.Reactive;
using System.Threading.Tasks;

namespace AvaloniaApplication.ViewModels
{
    public class MainWindowViewModel : ViewModelBase
    {
        string buttonText = "Click Me!";

        public string ButtonText
        {
            get { return buttonText; }

            set
            {
                this.RaiseAndSetIfChanged(ref buttonText, value);
                //buttonText = value;
                //this.RaisePropertyChanged(nameof(ButtonText));
            }
        }

        bool _isTheThingEnabled = true;
        public bool IsTheThingEnabled
        {
            get => _isTheThingEnabled;
            set => this.RaiseAndSetIfChanged(ref _isTheThingEnabled, value);
        }

        private double _floatValue = 9999.9999;
        public double FloatValue
        {
            get => _floatValue;
            set => this.RaiseAndSetIfChanged(ref _floatValue, value);
        }

        public string Greeting => "Welcome to Avalonia!";

        public void ButtonClicked() => ButtonText = "Hello, Avalonia!";

        public ReactiveCommand<string, Unit> DotheThing { get; }

        public void RunTheThing(string ob)
        {
            ButtonText = ob.ToString();
        }

        [DependsOn(nameof(IsTheThingEnabled))]
        public bool CanRunTheThing(string ob)
        {
            return IsTheThingEnabled && ob != null;
        }

        public MainWindowViewModel()
        {
            DotheThing = ReactiveCommand.Create<string>(RunTheThing);

            var textBlock = new TextBlock();
            var text = textBlock.GetObservable(TextBlock.TextProperty);
            text.Subscribe(value => Console.WriteLine(value + "changed"));

        }

        public Task<string> MyAsyncText => GetTextAsync();

        private async Task<string> GetTextAsync()
        {
            await Task.Delay(1000); // The delay is just for demonstration purpose
            return "Hello from async operation";
        }

        private ShapeType _SelectedShape;

        /// <summary>
        /// Gets or sets the selected ShapeType
        /// </summary>
        public ShapeType SelectedShape
        {
            get { return _SelectedShape; }
            set { this.RaiseAndSetIfChanged(ref _SelectedShape, value); }
        }

        /// <summary>
        ///  Gets an array of all available ShapeTypes
        /// </summary>
        public ShapeType[] AvailableShapes { get; } = Enum.GetValues<ShapeType>();
    }
}